'''
协程
'''
import asyncio
import time

#1
# async def func1():
#     print("你好啊，啾啾！")
#     await asyncio.sleep(3)
#     print("你好啊，啾啾！")
# async def func2():
#     print("你好啊，阿桃！")
#     await asyncio.sleep(2)
#     print("你好啊，阿桃！")
# async def func3():
#     print("你好啊，奶茶！")
#     # time.sleep(1) 同步操作
#     await asyncio.sleep(1) #异步睡眠 + 挂起
#     print("你好啊，奶茶！")
#
# if __name__ == '__main__':
#     tasks = [func1(), func2(), func3()] #异步协程函数，得到一个协程对象
#     #遇见同步操作，异步中断
#     asyncio.run(asyncio.wait(tasks))

#2
# async def func1():
#     print("你好啊，啾啾！")
#     await asyncio.sleep(3)
#     print("你好啊，啾啾！")
# async def func2():
#     print("你好啊，阿桃！")
#     await asyncio.sleep(2)
#     print("你好啊，阿桃！")
# async def func3():
#     print("你好啊，奶茶！")
#     # time.sleep(1) 同步操作
#     await asyncio.sleep(1) #异步睡眠 + 挂起
#     print("你好啊，奶茶！")
#
# async def main():
#     tasks = [func1(), func2(), func3()]
#     await asyncio.wait(tasks)
#
# if __name__ == '__main__':
#     asyncio.run(main())

#爬虫应用
async def download(url):
    print("开始下载")
    await asyncio.sleep(2) #网络请求
    print("下载结束")

async def main():
    urls = [
        "http://www.baidu.com",
        "http://www.blibli.com",
        "http://www.vip.com"
    ]
    tasks = []
    for url in urls:
        d = download(url)
        tasks.append(asyncio.create_task(d))

    await asyncio.wait(tasks)

if __name__ == '__main__':
    asyncio.run(main())